package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IColorDao;
import com.hamizhong.iflowers.db.dao.hibernate.IProductDao;
import com.hamizhong.iflowers.db.dao.hibernate.IVarietyDao;
import com.hamizhong.iflowers.db.entity.Color;
import com.hamizhong.iflowers.db.entity.Product;
import com.hamizhong.iflowers.db.entity.Variety;
import com.hamizhong.iflowers.service.IProductService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.ColorVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.ProductVO;
import com.hamizhong.iflowers.web.vo.VarietyVO;

public class ProductServiceImpl extends MyBaseService implements IProductService {
	
	private IProductDao iProductDao = null;
	private IVarietyDao iVarietyDao = null;
	private IColorDao iColorDao = null;

	public IProductDao getiProductDao() {
		return iProductDao;
	}
	public void setiProductDao(IProductDao iProductDao) {
		this.iProductDao = iProductDao;
	}
	public IVarietyDao getiVarietyDao() {
		return iVarietyDao;
	}
	public void setiVarietyDao(IVarietyDao iVarietyDao) {
		this.iVarietyDao = iVarietyDao;
	}
	public IColorDao getiColorDao() {
		return iColorDao;
	}
	public void setiColorDao(IColorDao iColorDao) {
		this.iColorDao = iColorDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<Product> productList = new ArrayList<Product>();
		productList = iProductDao.findProduct(pageNum, pageSize);
		int totalProductCnt = iProductDao.findTotalProductCnt();
		List<ProductVO> productVOList = new ArrayList<ProductVO>();
		ProductVO productVO = null;
		Variety variety = null;
		Color color = null;
		VarietyVO varietyVO = null;
		ColorVO colorVO = null;
		for (Product product : productList) {
			productVO = new ProductVO();
			BeanUtils.copyProperties(product, productVO);
			//封装上产品对应的花材和颜色
			variety = iVarietyDao.findById(product.getVariety().getVarietyId());
			color = iColorDao.findById(product.getColor().getColorId());
			varietyVO = new VarietyVO();
			colorVO = new ColorVO();
			BeanUtils.copyProperties(variety, varietyVO);
			BeanUtils.copyProperties(color, colorVO);
			productVO.setVarietyVO(varietyVO);
			productVO.setVarietyName(variety.getVarietyName());
			productVO.setColorVO(colorVO);
			productVO.setColorName(color.getColorName());
			productVOList.add(productVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setProductVOList(productVOList);
		iObjectVO.setTotalProductCnt(totalProductCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	
	public IOPResultDto<IObjectVO> addProduct(ProductVO productVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Product product = new Product();
		try {
			BeanUtils.copyProperties(productVO, product);
			Variety varietyTemp = new Variety();
			Color colorTemp = new Color();
			varietyTemp = iVarietyDao.findById(productVO.getVarietyVO().getVarietyId());
			colorTemp = iColorDao.findById(productVO.getColorVO().getColorId());
			product.setVariety(varietyTemp);
			product.setVarietyName(varietyTemp.getVarietyName());
			product.setColor(colorTemp);
			product.setColorName(colorTemp.getColorName());
			iProductDao.add(product);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	
	public IOPResultDto<IObjectVO> updateProduct(ProductVO productVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Product product = null;
		try {
			product = iProductDao.findById(productVO.getProductId());
			product.setProductName(productVO.getProductName());
			product.setSortId(productVO.getSortId());
			product.setRemarks(productVO.getRemarks());
			iProductDao.update(product);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	
	public IOPResultDto<IObjectVO> deleteProduct(ProductVO productVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			Integer productId = productVO.getProductId();
			Product product = iProductDao.findById(productId);
			if(null != product) {
				this.iProductDao.delete(product);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
